package com.power.web.action.data;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.power.model.dataDictionary.TddData;
import com.power.services.dataDictionary.DataDictionaryService;
import com.power.util.RequestUtil;
import com.power.web.action.base.AbstractBaseAction;

@SuppressWarnings("serial")
public class DataPluginAction extends AbstractBaseAction {

	@Autowired
	private DataDictionaryService dataService;
	
	@SuppressWarnings("unchecked")
	public String buildJsonData() {
		try {
			String tablename = RequestUtil.getParam(this.request, "tablename", "");
			String jsonString = "[{";
			jsonString += "\"result\" : ";
			List list = dataService.findAllDataByCode(tablename);
			if (null == list)
				list = new ArrayList();
			jsonString += "'[";
			for (int num = 0; num < list.size(); num++) {
				TddData data = (TddData) list.get(num);
				String code = data.getCode();
				String name = data.getName();
				if (num != 0)
					jsonString += ",";
				jsonString += "{\"code\":\"" + code + "\",\"name\":\"" + name + "\"}";
			}
			jsonString += "]'";
			jsonString += "}]";
			this.response.reset();
			this.response.setCharacterEncoding("UTF-8");
			this.response.getWriter().print(jsonString);
		} catch (IOException e) {
			e.printStackTrace();
			log.error("输出JSON数据错误！", e);
		}
		return null;
	}
}
